package controller;

import org.aspectj.weaver.ast.Or;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import po.*;
import service.OrderService;
import utils.ResUtils;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;


@Controller
public class OrderController {
    @Autowired
    private OrderService orderService;
    // 获取订单列表数据
    @GetMapping("/orders")
    @ResponseBody
    public Object AllOrder(HttpServletRequest req) {
        List<Order> res;
//        stuName=&proId=&status=&admId=
        String stuname = req.getParameter("stuName").trim();
        String proId = req.getParameter("proId").trim();
        String status = req.getParameter("status").trim();
        String admId = req.getParameter("admId").trim();
//        System.out.println("获取到的数据"+stuname+" "+proId+" "+status+" " +admId);

        Order order = new Order();


        if (stuname != "") {
          Student student = new Student();
          student.setUsername(stuname);
          order.setStudent(student);
        }
        if (proId != "") {
            Product product = new Product();
            product.setId(Integer.parseInt(proId));
            order.setProduct(product);
        }
        if (admId != "") {
            Admin admin = new Admin();
            admin.setId(Integer.parseInt(admId));
            order.setAdmin(admin);
        }

            res = this.orderService.allOrder(order,status);



        if (res != null) {
            return ResUtils.Success(1,"查询成功",res);
        } else {
            return ResUtils.Fail(0,"没有数据");
        }

    }

    // 获取订单列表数据 注册时间在范围内 却 区分一下客户类型
    @PostMapping("/orders/chart")
    @ResponseBody
    public Object AllOrderChart(@RequestBody Order order) {

        System.out.println(order.getProduct().getId()+ "获取的产品id");
        System.out.println(order.getStudent().getLevel().getId()+ "获取的类型id");
        System.out.println(order.getCreateTime()+ "获取的开始时间");
        System.out.println(order.getUpdateTime()+ "获取的结束时间");
        List<Order> res = this.orderService.allOrderChart(order);

        if (res != null) {
            return ResUtils.Success(1,"查询成功",res);
        } else {
            return ResUtils.Fail(0,"没有数据");
        }

    }

    // 增加订单
    @PostMapping("/orders")
    public String addOrder(@RequestBody  Order order, RedirectAttributes attr) {
        System.out.println("接收到增加订单请求");
        Date now = new Date();
        // 创建
        order.setCreateTime(now);
        // 最近缴费
        order.setRenewTime(now);
        // 更新
        order.setUpdateTime(now);


        int id = orderService.addOrder(order);

        if (id > 0 ) {
            attr.addAttribute("code",1);
            attr.addAttribute("orderId",order.getId());
            attr.addAttribute("money",order.getTotalMoney());
        } else {
            attr.addAttribute("code",0);
            attr.addAttribute("orderId",order.getId());
            attr.addAttribute("money",order.getTotalMoney());
        }
        // 转发到修改订单表的总金额
        System.out.println("转发到修改订单表的总金额");
        return "redirect:/payments/order";
    }

    // 删除订单
    @DeleteMapping("/orders/{id}")
    @ResponseBody
    public Object deleteOrder(@PathVariable("id") Integer id) {
        // 删除对应的充值记录
        boolean flag =orderService.deleteOrderOfPayment(id);
        if(flag){
            // 删除对应订单
            boolean res = orderService.deleteOrder(id);

            if (res) {
                return ResUtils.Success(1,"删除成功");
            } else {
                return ResUtils.Fail(0,"删除失败");
            }
        }else {
            return ResUtils.Fail(0,"删除失败");
        }

    }

    // 根据 id 查询订单信息
    @GetMapping("/orders/{id}")
    @ResponseBody
    public Object orderOfId(@PathVariable("id") Integer id) {

//        System.out.println("获取id为" + id + "订单");
        Order order = orderService.orderOfId(id);
        if (order !=null) {
            return ResUtils.Success(1, "查询成功",order);
        } else {
            return ResUtils.Fail(0, "查询失败不存在 Id 为 "+id+" 的学生信息");
        }

    }

    // 根据 id 修改订单信息
    @PutMapping("/orders/{id}")
    @ResponseBody
    public Object editOrder(@PathVariable("id") Integer id, @RequestBody Order order) {

//        Date now = new Date();
//        student.setUpdateTime(now);
//
//        System.out.println(student);
//
        Boolean res = orderService.editOrder(order);
        System.out.println(id+"asdfads");
        System.out.println(order.getId());
        System.out.println(order.getStudent().getId());
        System.out.println(order.getAdmin().getId());
        System.out.println(order.getUpdateTime());
        System.out.println(order.getProduct().getId());
//
//        Boolean res = true;
        if (res) {
            return ResUtils.Success(1, "修改成功",order);
        } else {
            return ResUtils.Fail(0, "修改失败");
        }
    }



    // 根据 id 修改 总金额订单信息
    @GetMapping("/orders/payment")
    @ResponseBody
    public Object editTotolMoney(@RequestParam("code") Integer code,
                                 @RequestParam("orderId") Integer orderId,
                                 @RequestParam("money") Double money) {

        System.out.println("获取id为" + orderId + "订单" + code + money);
        //根据 id 获取订单
        Order order = this.orderService.orderOfId(orderId);
        //  修改结束时间
        if(money == 300 ) {
            Date endTime = order.getEndTime();
            Calendar cal = Calendar.getInstance();
            cal.setTime(endTime);
            // 加一年
            cal.add(Calendar.YEAR,1);
            order.setEndTime(cal.getTime());
        }else {
            Date endTime = order.getEndTime();
            Calendar cal = Calendar.getInstance();
            cal.setTime(endTime);
            // 加一月
            cal.add(Calendar.MONTH,1);
            order.setEndTime(cal.getTime());
        }
        // 修改最近充值时间
        Date now = new Date();
        order.setRenewTime(now);
        // 修改更新时间
        order.setUpdateTime(now);
        order.setId(orderId);
        // 设置增加的金额
        order.setTotalMoney(money);
        System.out.println(order.getEndTime());
        if (code == 1) {
            Boolean res = this.orderService.editTotolMoney(order);
            if (res) {
                return ResUtils.Success(1, "续费成功",order);
            } else {
                return ResUtils.Fail(0, "续费失败");
            }
        }else {
            return ResUtils.Fail(0, "续费失败");
        }
    }

}
